//
//  CZPictureView.swift
//  WeiBo
//
//  Created by apple on 16/6/30.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
// cell 可重用标识符
private let pictureViewCellId = "pictureViewCellId"
// cell 之间的间距
private let itemMargin: CGFloat = 5
// 计算cell的宽高
private let cellWH = (SCREENW - StautsMargin*2 - itemMargin*2) / 3

class CZPictureView: UICollectionView {

    // 声明一个配图数组属性供外界赋值
    var pic_urls: [CZPictureInfo]?{
        didSet{

            // 给countLabel设置text
            countLabel.text = "\(pic_urls?.count ?? 0)"
            let size = getPictureViewSize(pic_urls?.count ?? 0)
            // 给其设置约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            // 刷新UI
            reloadData()
        }
    }
    // 通过多少张配图计算配图size
    func getPictureViewSize(count:Int) -> CGSize{
        
        
        // 计算行数 和列数
        let col = count == 4 ? 2 : (count >= 3 ? 3 : count)
        let row = count == 4 ? 2 : ((count - 1) / 3 + 1)
        // 计算配图宽高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1)*itemMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1)*itemMargin
        
        return CGSize(width: w, height: h)
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setupUI(){
        backgroundColor = UIColor.clearColor()
        
        // 注册cell
        registerClass(CZPictureViewCell.self, forCellWithReuseIdentifier: pictureViewCellId)
        // 设置数据源
        dataSource = self
        // 设置layout
        let layout = collectionViewLayout as!UICollectionViewFlowLayout
        // 设置item
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        // 设置垂直间距和水平间距
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        // 添加控件
        addSubview(countLabel)
        // 添加约束
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
    }
    // 加载控件
    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.redColor(), textFont: 35)
}
extension CZPictureView: UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewCellId, forIndexPath: indexPath) as! CZPictureViewCell
   
        // 给cell赋值
        cell.picInfo = pic_urls![indexPath.item]
        return cell
    }
    
}
// 自定义cell
class CZPictureViewCell: UICollectionViewCell {
    // 供外界给其赋值
    var picInfo: CZPictureInfo?{
        didSet{
            // 设置图片
            bgImageView.sd_setImageWithURL(NSURL(string: picInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        // 添加控件
        contentView.addSubview(bgImageView)
        // 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
    }
    
    // 加载控件
    private lazy var bgImageView: UIImageView = {
        let img = UIImageView(imgName: "timeline_card_top_background")
        // 指定图片填充方式
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        return img
        
    }()
}










